package com.ztjava.weixin.controller;

import com.jfinal.aop.Duang;
import com.jfinal.plugin.redis.Cache;
import com.jfinal.plugin.redis.Redis;
import com.ztjava.core.Constant;
import com.ztjava.core.annotation.ControllerClass;
import com.ztjava.core.base.BaseController;
import com.ztjava.core.client.PortalClient;
import com.ztjava.ext.auth.annotation.UserController;
import com.ztjava.ext.auth.annotation.UserMethod;
import com.ztjava.ext.auth.bean.AuthSession;
import com.ztjava.weixin.model.message.MessageReply;
import com.ztjava.weixin.model.message.MessageRule;
import com.ztjava.weixin.service.MessageService;
@ControllerClass(client=PortalClient.class,path="/weixin/message")
@UserController(portal=Constant.P_PORTAL)
public class MessagePortalController extends BaseController {
	/*************
	 *MessageAdminController.java
	 *@ztjava.com
	 *2018-07-09 21:03:38
	**************/
	private static MessageService messageService = Duang.duang(MessageService.class);
	/*************
	 *修改Reply
	**************/
	@UserMethod("WeixinMessageReply")
	public void addEditReply(){
		Integer id = getParaToInt("id",0);
		MessageReply messageReply = new MessageReply();
		if(id>0){
			messageReply = MessageReply.dao.findById(id);
		}
		setAttr("messageReply",messageReply);
		render("addEditReply.html");
	}

	/*************
	 *保存Reply
	**************/
	@UserMethod("WeixinMessageReply")
	public void saveReply(){
		AuthSession authSession = getAttr(Constant.AUTHSESSION);
		MessageReply messageReply = getModel(MessageReply.class);
		saveModel(messageReply,authSession.getId());
		Cache cache = Redis.use();
		cache.hdel(Constant.R_PRE+"WEIXIN:MESSAGE:REPLY", messageReply.getInt("id").toString());
		renderSuccess();
	}

	/*************
	 *Reply列表
	**************/
	@UserMethod("WeixinMessageReply")
	public void replyList(){
		
		if(!getPara("is_ajax","F").equals("T")){
			keepPara();
			render("replyList.html");
			return;
		}
		renderPageJson(messageService.getReplyPage(client, getParaMap()));
	}

	/*************
	 *批量删除Reply
	**************/
	@UserMethod("WeixinMessageReply")
	public void batchDeleteReply(){
		String[] btSelectItem = getParaValues("btSelectItem");
		for(String item:btSelectItem){
			messageService.deleteReply(Integer.parseInt(item));
		}
		messageService.loadRuleCache();
		renderSuccess();
	}

	/*************
	 *修改Rule
	**************/
	@UserMethod("WeixinMessageReply")
	public void addEditRule(){
		Integer id = getParaToInt("id",0);
		MessageRule messageRule = new MessageRule();
		if(id>0){
			messageRule = MessageRule.dao.findById(id);
		}else{
				messageRule.set("reply_id",getParaToInt("reply_id",0));
		}
		setAttr("messageRule",messageRule);
		render("addEditRule.html");
	}

	/*************
	 *保存Rule
	**************/
	@UserMethod("WeixinMessageReply")
	public void saveRule(){
		AuthSession authSession = getAttr(Constant.AUTHSESSION);
		MessageRule messageRule = getModel(MessageRule.class);
		saveModel(messageRule,authSession.getId());
		messageService.loadRuleCache();
		renderSuccess();
	}

	/*************
	 *Rule列表
	**************/
	@UserMethod("WeixinMessageReply")
	public void ruleList(){
		Integer reply_id = getParaToInt("reply_id");
		setAttr("reply_id",reply_id);
		if(!getPara("is_ajax","F").equals("T")){
			keepPara();
			render("ruleList.html");
			return;
		}

		
		renderPageJson(messageService.getRulePage(client, getParaMap()));
	}

	/*************
	 *批量删除Rule
	**************/
	@UserMethod("WeixinMessageReply")
	public void batchDeleteRule(){
		String[] btSelectItem = getParaValues("btSelectItem");
		messageService.batchDelete(MessageRule.tablename, btSelectItem);
		messageService.loadRuleCache();
		renderSuccess();
	}


}
